<template>
  <div>
    <el-card class="box-card">
      <div class="logo">
        M
      </div>
      <h3>使用 XMall 账号 登录官网</h3>
      <!-- 表单 -->
      <el-form :model="addRuleForm"
               :rules="addFormRules"
               ref="addFormRef">
        <el-form-item prop="username">
          <el-input placeholder="账号"
                    v-model="addRuleForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input placeholder="密码"
                    v-model="addRuleForm.password"></el-input>
        </el-form-item>
      </el-form>
      <span class="clearfix checkBox">
        <el-checkbox v-model="checked">记住密码</el-checkbox>
        <span class="register"><a href="javascript:;">注册 XMall 账号</a><span>|</span><a href="javascript::">忘记密码
            ?</a></span>
      </span>
      <div class="butn">
        <el-button type="primary"
                   @click="login">登录</el-button>
        <el-button @click="goBack">返回</el-button>
      </div>
      <div class="other">
        <el-row>
          <span><b>其他账号登录</b><i class="el-icon-help"></i><i class="el-icon-watermelon"></i><i class="el-icon-orange"></i></span>
        </el-row>
      </div>
    </el-card>

  </div>
</template>
<script>
export default {
  data() {
    return {
      // 单选框，默认false
      checked: false,
      // 表单验证
      addRuleForm: {
        username: '',
        password: ''
      },
      // 验证规则
      addFormRules: {
        username: [
          { required: true, message: '请输入正确的账号', trigger: 'blur' },
          { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入正确的密码', trigger: 'blur' },
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 点击登录
    login() {
      this.$refs.addFormRef.validate(async (valid) => {
        if (!valid) return false
        const res = await this.$http.post('/api/login', this.addRuleForm)
        if (res.status !== 200) {
          return this.$Message.error('登陆失败')
        }
        console.log(res.data)
        this.$Message.success('登陆成功')
        // 1. 将登录成功之后的 token，保存到客户端的 sessionStorage 中
        //   1.1 项目中出了登录之外的其他API接口，必须在登录之后才能访问
        //   1.2 token 只应在当前网站打开期间生效，所以将 token 保存在 sessionStorage 中
        window.sessionStorage.setItem('token', res.data.token)
        window.sessionStorage.setItem('userId', res.data.id)
        this.$router.push('/home')
      })
    },
    goBack() {
      this.$router.push('/home')
    }
  }
}
</script>

<style lang="less" scoped>
.box-card {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 452px;
  transform: translate(-50%, -50%);
  padding: 30px 0;

  .logo {
    height: 100px;
    width: 100px;
    background-color: orangered;
    border-radius: 50%;
    text-align: center;
    line-height: 100px;
    color: white;
    font-size: 60px;
    display: block;
    margin: 0 auto;
  }

  .el-form {
    padding: 0 28px;
  }

  h3 {
    margin: 14px 0;
    text-align: center;
  }

  .el-checkbox {
    padding: 0 15px !important;
    margin: 20px 0;
  }

  .checkBox {
    padding: 15px;

    .register {
      margin-left: 80px;

      a {
        margin: 0 10px;
        color: blue;
      }
    }
  }

  .butn {
    text-align: center;
    padding-bottom: 20px;

    .el-button {
      width: 372px;
      margin-bottom: 15px;
      margin-left: 0;
      height: 50px;
    }
  }

  .other {
    height: 41px;
    width: 370px;
    border-top: 1px solid #ccc;
    margin: 0 auto;
    line-height: 41px;

    span {
      b {
        color: #999;
        font-size: 14px;
        margin-right: 10px;
      }

      i {
        margin-left: 10px;
        display: inline-block;
        vertical-align: middle;
        color: #999;
      }
    }
  }
}
</style>
